﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace LCRReader
{
    public partial class usrSpec : UserControl
    {
       
        TestConfigAccess db = new TestConfigAccess();
     
        [Description("编号")]
        public int ID { get; set; }
        [Description("测试项目")]
        public string Item
        {
            get { return this.richTextBox1.Text; }
            set {this.richTextBox1.Text = value;  }
        }

        [Description("测试项目")]
        public string CMD
        {
            get { return this.textBox3.Text; }
            set { this.textBox3.Text = value; }
        }
        public usrSpec()
        {
            InitializeComponent();
        }
        [Description("上限")]
        public float UpperValue
        {
            get
            {
                float a = float.NaN;
                float.TryParse(edtUp.Text, out a);
                return a;
            }
            set { edtUp.Text = value.ToString(); }
        }
        [Description("下限")]
        public float LowerValue
        {
            get
            {
                float a = float.NaN;
                float.TryParse(edtlow.Text, out a);
                return a;
            }
            set { edtlow.Text = value.ToString(); }
        }
        [Description("测试值")]
        public float TestValue
        {
            get
            {
                float a = float.NaN;
                float.TryParse(edtValue.Text, out a);
                return a;
            }
            set { edtValue.Text = value.ToString(); }
        }
        [Description("测试结果"),DefaultValue(-1)]
        public int Result
        {
            get
            {
                if (lblResult.Text == "PASS")
                    return 0;
                else if (lblResult.Text == "FAIL")
                    return 1;
                else
                    return -1;
            }
            set
            {
                if (value==0)
                {
                    lblResult.BackColor = Color.Green;
                    lblResult.Text = "PASS";
                }
                else if(value==1)
                {
                    lblResult.BackColor = Color.Red;
                    lblResult.Text = "FAIL";
                }
                else
                {
                    lblResult.BackColor = Color.Gray;
                    lblResult.Text = "UNTEST";
                }
            }
        }
        [Description("是否有效")]
        public bool Enable
        {
            get
            {
                return chkEnable.Checked;
            }
            set
            {
                chkEnable.Checked = value;
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            db.DeleteById(ID);
            if(UpdateUI!=null)
            UpdateUI();
        }
        public event Action UpdateUI;

        private void chkEnable_CheckedChanged(object sender, EventArgs e)
        {
            db.UpdateEnable(ID, this.chkEnable.Checked);
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmConfig frmConfig = new frmConfig { Text = "编辑" };
            frmConfig.textBox1.Text = Item;
            frmConfig.textBox2.Text = UpperValue.ToString();
            frmConfig.textBox3.Text = LowerValue.ToString();
            frmConfig.textBox4.Text = CMD;
            frmConfig.checkBox1.Checked = Enable;
            frmConfig.idUpdate = ID;
            frmConfig.StartPosition = FormStartPosition.CenterScreen;
            frmConfig.Update += UpdateUI;
            frmConfig.Show();
        }

        private void lblResult_Click(object sender, EventArgs e)
        {
            Test();
        }

        public void Test()
        {
            float v = float.NaN;
            if (Item == "电阻")
            {
                TestValue = (float)Math.Round(global.Voltage / global.Current, 3);
                global.R = TestValue;
                v = TestValue;
                if ((v < UpperValue) && (v > LowerValue))
                {
                    Result = 0;
                    
                }
                else
                {
                    Result = 1;
                }

                global.RResult = (Result==0)?true:false;
            }
            else
            {
                if (Comm.Inst.GetValue(CMD, out v) == 0)
                {
                    TestValue = v;
                    if (Item == "电流")
                    {
                        global.Current = v;
                        if ((v < UpperValue) && (v > LowerValue))
                        {
                            Result = 0;
                        }
                        else
                        {
                            Result = 1;
                        }
                        global.IResult = (Result == 0) ? true : false;
                    }
                    else if (Item == "电压")
                    {
                        global.Voltage = v;
                        if ((v < UpperValue) && (v > LowerValue))
                        {
                            Result = 0;
                        }
                        else
                        {
                            Result = 1;
                        }
                        global.UResult = (Result == 0) ? true : false;
                    }

                }
                else
                {
                    Result = -1;
                    MessageBox.Show("Error:" + CMD);
                }
            }
        }
    }
}
